package com.cube.dp.cor.filter;

import com.cube.dp.cor.context.TransformContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cube.li
 * @date 2021/12/13 21:50
 * <p>
 * 默认实现的短链转换过滤器链
 */
@Slf4j
public class DefaultTransformFilterChain implements TransformFilterChain {

    private List<TransformFilter> filters = new LinkedList<>();

    private TransformContext context;


    public DefaultTransformFilterChain(TransformContext context) {
        this.context = context;
    }


    @Override
    public void doFilter() {
        filters.forEach(filter -> filter.doFilter(context));
    }

    /**
     * 添加过滤器,并将其加入合适的位置(根据order升序排序)
     *
     * @param filter 待添加的过滤器
     */
    public void addTransformFilter(TransformFilter filter) {
        //根据order添加到合适的位置
    }

    /**
     * 添加过滤器
     *
     * @param filters 集合
     */
    public void addTransformFilters(List<TransformFilter> filters) {
        this.filters.addAll(filters.stream().sorted((Comparator.comparingInt(TransformFilter::getOrder))).collect(Collectors.toList()));
    }

    public List<TransformFilter> getFilters() {
        return filters;
    }

    public TransformContext getContext() {
        return context;
    }
}
